package com.app.ui.activity.account;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.app.net.res.user.User;
import com.app.ui.activity.action.NormalActionBar;
import com.app.utiles.other.StringUtile;
import com.app.utiles.other.ToastUtile;
import com.wxj.zx.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.listener.SaveListener;


/**
 * Created by Administrator on 2016/2/15.
 */
public class RegisterActivity extends NormalActionBar {
    @Bind(R.id.register_phone_et)
    EditText registerPhoneEt;
    @Bind(R.id.register_password_et)
    EditText registerPasswordEt;
    @Bind(R.id.register_password2_et)
    EditText registerPassword2Et;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registe);
        setBarColor();
        ButterKnife.bind(this);
        setBarBack();
        setBarTvText(1, "注册");

    }


    @OnClick(R.id.register_btn)
    public void onViewClicked() {
        String phone = registerPhoneEt.getText().toString();
        String password = registerPasswordEt.getText().toString();
        String password2 = registerPassword2Et.getText().toString();
        if (!StringUtile.isPhone(phone)) {
            ToastUtile.showToast("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(password) || password.length() < 6) {
            ToastUtile.showToast("请输入6-20位密码");
            return;
        }
        if (!password.equals(password2)) {
            ToastUtile.showToast("2次输入的密码不一致");
            return;
        }
        register(phone, password);
    }

    private void register(String phone, String password) {
        User user = new User();
        user.setMobilePhoneNumber(phone);
        user.setPassword(password);
        user.setUsername(phone);
        dialogShow();
        user.signUp(this, new Listener());

    }

    class Listener extends SaveListener {

        @Override
        public void onSuccess() {
            ToastUtile.showToast("注册成功");
            dialogDismiss();
            finish();
        }

        @Override
        public void onFailure(int i, String s) {
            dialogDismiss();
            String msg = "注册失败";
            if (i == 202) {
                msg = "该账号已经存在，您可以直接登录";
            }
            ToastUtile.showToast(msg);
        }
    }
}
